package com.ych.utils;

import com.ych.https.SSLClient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/5
 * Time: 15:16
 * Description:
 */
public class HttpsUtils {
    /**
     * 发送post请求
     *
     * @param url
     * @param header
     * @param data
     * @return
     */
    public static String HttpsPost(String url, Map<String, Object> header, String data) {
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-Type", "application/json");
        post.setHeader("charset", "UTF-8");
        for (String key : header.keySet()) {
            Object val = header.get(key);
            if (null == val) {
                val = "";
            }
            post.setHeader(key, val.toString());
        }
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000)
                .setSocketTimeout(5000).build();
        post.setConfig(requestConfig);
        HttpClient httpclient = null;
        HttpResponse httpResponse;
        try {
            StringEntity s = new StringEntity(data, "UTF-8");
            post.setEntity(s);
            httpclient = new SSLClient();
            httpResponse = httpclient.execute(post);
            HttpEntity entity = httpResponse.getEntity();
            String result = EntityUtils.toString(entity);
            System.out.println(result);
            return result;
        } catch (Exception e) {

        } finally {
            try {
                if (null != httpclient) {
                    ((SSLClient) httpclient).close();
                }
            } catch (Exception e) {

            }
        }
        return null;
    }

    /**
     * 发送get请求
     *
     * @param url
     * @param header
     * @return
     */
    public static String HttpsGet(String url, Map<String, Object> header) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        for (String key : header.keySet()) {
            Object val = header.get(key);
            if (null == val) {
                val = "";
            }
            httpget.setHeader(key, val.toString());
        }
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpget);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String result = null;
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
                result = new String(result.getBytes(Charset.forName("UTF-8")));
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}